package admin.controllers;

import helpers.CookieHelper;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import entities.Application;

import admin.models.ApplicationModel;


public class ApplicationDeleteServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		response.setContentType("text/html");
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = "";
		try {
			id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		} catch (Exception e) {
			response.sendRedirect("/admin/application/list");
		}
		
		try {
			ApplicationModel appModel = ApplicationModel.getInstance();
			Application app = appModel.get(id);
			appModel.delete(app);
			
			CookieHelper.setCookie(response, "success", "Delete success app " + app.getId());
		} catch (Exception e) {
			// TODO: handle exception
		}
		response.sendRedirect("/admin/application/list");
	}
}
